package com.llyb.tingshu.account.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llyb.tingshu.account.service.UserAccountService;
import com.llyb.tingshu.common.login.TsLogin;
import com.llyb.tingshu.common.result.Result;
import com.llyb.tingshu.common.util.AuthContextHolder;
import com.llyb.tingshu.model.account.UserAccountDetail;
import com.llyb.tingshu.vo.account.AccountDeductVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"all"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;


	@TsLogin
	@Operation(summary = "消费列表")
	@GetMapping("/findUserConsumePage/{pageNo}/{pageSize}")
	public Result findUserConsumePage(@PathVariable("pageNo") Long pageNo,
									   @PathVariable("pageSize") Long pageSize) {


		Long userId = AuthContextHolder.getUserId();
		IPage<UserAccountDetail> pageModel = userAccountService.findUserConsumePage(new Page<>(pageNo, pageSize), userId);
		return Result.ok(pageModel);
	}


	/**
	 * 充值记录
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@TsLogin
	@Operation(summary = "充值记录")
	@GetMapping("/findUserRechargePage/{pageNo}/{pageSize}")
	public Result findUserRechargePage(@PathVariable("pageNo") Long pageNo,
									    @PathVariable("pageSize") Long pageSize){
		// 获取到用户Id
		Long userId = AuthContextHolder.getUserId();
		// 构建Page对象
		Page<UserAccountDetail> pageParam = new Page<>(pageNo, pageSize);
		// 调用服务层方法
		IPage<UserAccountDetail> pageModel = userAccountService.findUserRechargePage(pageParam, userId);
		// 返回数据
		return Result.ok(pageModel);
	}

	/**
	 * 检查及扣减账户余额
	 *
	 * @param accountDeductVo
	 * @return
	 */
	@Operation(summary = "检查及扣减账户余额")
	@PostMapping("/checkAndDeduct")
	public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo){

		Boolean flag = userAccountService.checkAndDeduct(accountDeductVo);

		return Result.ok(flag);
	}

	/**
	 * 获取用户余额
	 * @return
	 */
	@TsLogin
	@Operation(summary = "获取用户余额")
	@GetMapping("/getAvailableAmount")
	public Result getAvailableAmount(){

		Long userId = AuthContextHolder.getUserId();

		BigDecimal availableAmount = userAccountService.getAvailableAmount(userId);
		return Result.ok(availableAmount);
	}

}

